package com.orm;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created by lirance on 16/4/5.
 */
public class WebServiceDao extends HibernateDaoSupport {

    private static final Log log = LogFactory.getLog(UserDao.class);

    /**
     * get all of users
     *
     * @return the list of users
     */
    public List getAllEntities() {
        Session session = null;
        try {
            session = this.getSessionFactory().openSession();
            return session.createQuery("from WebServiceEntity ").list();
        } finally {
            session.close();
        }
    }

    public List getWebServiceEntityById(int id) {
        Session session = this.getSessionFactory().openSession();
        List entity = session.createQuery("from WebServiceEntity where wsId = " + id).list();
        session.close();
        if (entity.size() == 1) {
            return entity;
        } else {
            return null;
        }
    }

    public List getWebServiceEntityByName(String name) {
        Session session = this.getSessionFactory().openSession();
        List entity = session.createQuery("from WebServiceEntity where wsName = '" + name+"'").list();
        session.close();
        if (entity.size() == 1) {
            return entity;
        } else {
            return null;
        }
    }
    public List getWebServiceEntityByClusteName(String name) {
        Session session = this.getSessionFactory().openSession();
        List entity = session.createQuery("from WebServiceEntity where clusteNam = '" + name+"'").list();
        session.close();
        if (entity.size() == 1) {
            return entity;
        } else {
            return null;
        }
    }


    /**
     * Update the modified webServiceEntity
     *
     * @param webServiceEntity the modified WebServiceEntity
     */
    public void update(WebServiceEntity webServiceEntity) {
        Session session = this.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        session.update(webServiceEntity);
        transaction.commit();
        session.close();
    }

    public void updateClustName(String clusteName, int wsId) {
        Session session = this.getSessionFactory().openSession();
        session.beginTransaction();
        Query query = session.createQuery("update WebServiceEntity set clusteNam = '" + clusteName + "' where wsId = " + wsId);
        query.executeUpdate();
        session.getTransaction().commit();
        session.close();
    }

    public void updateClustNume(String clusteNume, int wsId) {
        Session session = this.getSessionFactory().openSession();
        session.beginTransaction();
        Query query = session.createQuery("update WebServiceEntity  w set w.clusteNum = '" + clusteNume + "' where wsId = " + wsId);
        query.executeUpdate();
        session.getTransaction().commit();
        session.close();
    }

    /**
     * delete the specified webServiceEntity
     *
     * @param webServiceEntity the object which will be deleted
     */
    public void delete(WebServiceEntity webServiceEntity) {
        Session session = this.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        session.delete(webServiceEntity);
        transaction.commit();
        session.close();
    }


    /**
     * get the userEntity by username
     *
     * @param id the user's id
     * @return the object if username is exists or null
     */
   /* public WebServiceEntity getWebServiceEntity(int id) {
        Session session = this.getSessionFactory().openSession();
        List entity = session.createQuery("from UserEntity where uId = " + id).list();
        if (entity.size() == 1) {
            return (WebServiceEntity) entity.get(0);
        } else {
            return null;
        }
    }*/


    /**
     * get all of the users
     *
     * @return the list of users
     */
    public List getAllWebService() {
        Session session = getSessionFactory().openSession();
        List list = null;
        try {
            list = session.createQuery("from WebServiceEntity ").list();
        } catch (Exception e) {
            list = session.createQuery("from WebServiceEntity ").list();
        }
        session.close();
        return list;
    }

    /**
     * insert a new object to the database
     *
     * @param webserviceEntityRef the object will be inserted into the database
     */
    public void insert(WebServiceEntity webserviceEntityRef) {
        Session session = this.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        session.save(webserviceEntityRef);
        transaction.commit();
        session.close();
    }


}
